<?php

namespace app\account\controller;

use app\BaseController;

use app\common\model\FirmAccountModel;
use app\common\model\FirmModel;

use think\facade\Cache;

class Firm extends BaseController
{
    public function list(){
        $keyword = $this->request->param('keyword/s','');
        $account_info = $this->request->account_info;
        $where = FirmModel::where('firm_name','like','%'.$keyword.'%');

        $list = FirmAccountModel::hasWhere('firm', $where)
            ->where('account_id', $account_info->account_id)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('create_time','desc')
            ->select()
            ->append(['firm']);
        $total = FirmAccountModel::hasWhere('firm', $where)->where('account_id', $account_info->account_id)->count('firm_account_id');
        $create_num = FirmAccountModel::where('account_id', $account_info->account_id)->where('role',1)->count('firm_account_id');
        return success([
            'list' => $list,
            'total' => $total,
            'create_num' => $create_num
        ]);
    }

    public function create($firm_name){
        $account_info = $this->request->account_info;
        if($account_info->role === 0){
            return error('无权限');
        }
        $num = FirmAccountModel::where([
            'account_id' => $account_info->account_info,
            'is_delete' => 0,
            'role' => 1
        ])->count('firm_account_id');
        
        if($account_info->num > $num || $account_info->num === 0){
            $firmModel = new FirmModel();
            $firmModel->firm_name = $firm_name;
            $firmModel->expire_time = $this->request->time() + 3600*24*30;
            if($firmModel->save()){
                $firmAccountModel = new FirmAccountModel();
                $firmAccountModel->firm_id = $firmModel->firm_id;
                $firmAccountModel->account_id = $account_info->account_id;
                $firmAccountModel->role = 1;
                if($firmAccountModel->save()){
                    return success('创建成功');
                }
            }else{
                return error('创建失败');
            }
            
        }else{
            return error('账号余量不足');
        }
        
    }
    public function login(){
        $firm_id = $this->request->param('firm_id/d',0);
        if(empty($firm_id)){
            return error('错误操作'.$firm_id);
        }
        if(null === $FirmModel = FirmModel::find($firm_id)){
            return error('账号不存在');
        }
        if($this->request->account_info->role < 5){
            $where = [
                'firm_id' => $firm_id,
                'account_id' => $this->request->account_info->account_id,
                'is_delete' => 0
            ];
            if(null == $FirmAccountModel = FirmAccountModel::where($where)->find()){
                return error('无权限');
            }
        }
        
        $admin_token = hash('md5', uniqid());
        $firm_info = [
            'account_id'=>$this->request->account_info->account_id,
            'firm_id' => $firm_id,
            'customer_service_id' => $FirmModel->customer_service_id,
            'role' => $this->request->account_info->role > 4 ? $this->request->account_info->role : $FirmAccountModel->role,
            'firm_type' => $FirmModel->type
            ];
        Cache::set('admin_token_'.$admin_token,$firm_info);
        $url = 'https://'.$_SERVER['SERVER_NAME'].'/admin/#/auth?admin_token='.$admin_token;
        return success(['url'=>$url]);
    }
}
